package com.fzh.wangxiao.web.order.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzh.wangxiao.admin.service.UserService;
import com.fzh.wangxiao.utils.Result;
import com.fzh.wangxiao.web.member.service.TBizMemberService;
import com.fzh.wangxiao.web.order.entity.TBizOrder;
import com.fzh.wangxiao.web.order.mapper.TBizOrderMapper;
import com.fzh.wangxiao.web.order.service.TBizOrderService;
import com.fzh.wangxiao.web.req.OrderListRequest;
import com.fzh.wangxiao.web.req.OrderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-01-05
 */
@Service
public class TBizOrderServiceImpl extends ServiceImpl<TBizOrderMapper, TBizOrder> implements TBizOrderService {


    @Autowired
    private UserService userService;
    @Autowired
    private TBizMemberService memberService;

    @Override
    public Result list(OrderListRequest request) {
        Result result = new Result();
        QueryWrapper<TBizOrder> wrapper = new QueryWrapper();
        wrapper.orderByDesc("create_time");
        Page<TBizOrder> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<TBizOrder> mapIPage = baseMapper.selectPage(pages, wrapper);
        for(int i =0;i<mapIPage.getRecords().size();i++){
            mapIPage.getRecords().get(i).setMemberName(memberService.getMemberName(mapIPage.getRecords().get(i).getMemberId()));
            mapIPage.getRecords().get(i).setUserName(userService.getUserName(mapIPage.getRecords().get(i).getUserId()));
        }
        result.setData(mapIPage.getRecords());
        return result;
    }


    @Override
    public Result update(OrderRequest request) {
        Result result = new Result();
        TBizOrder order =  new TBizOrder();
        order.setId(Integer.parseInt(request.getId()));
        order.setAmount(request.getAmount());
        order.setCommission(request.getCommission());
        order.setProfit(request.getProfit());
        order.setGrossProfit(request.getGrossProfit());
        order.setStatus(Integer.parseInt(request.getStatus()));
        baseMapper.updateById(order);
        return result;
    }


    @Override
    public void insert(TBizOrder order) {

        baseMapper.insert(order);
    }
}
